package com.hy.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hy.domain.Blog;
import com.hy.domain.Section;
import com.hy.domain.dto.R;
import com.hy.service.BlogService;
import com.hy.service.SectionService;
import com.hy.mapper.SectionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author hou
* @description 针对表【section】的数据库操作Service实现
* @createDate 2023-04-23 19:05:24
*/
@Service
public class SectionServiceImpl extends ServiceImpl<SectionMapper, Section>
    implements SectionService{

    @Autowired
    private BlogService blogService;

    @Override
    public R add(Section section) {
        String title = section.getTitle();
        if (StrUtil.isEmpty(title)) {
            return R.fail("板块名称为空");
        }

        final Integer count = lambdaQuery().eq(Section::getTitle, section.getTitle()).count();
        if (count != null || count > 0 ) {
            return R.fail("此板块已经存在");
        }
        save(section);
        return R.ok("添加成功");
    }

    @Override
    public R deleteById(int id) {
        List<Blog> blog = blogService.listBySectionId(id);
        if (blog.size() > 0) {
            return R.fail("当前板块下存在博客");
        }
        return R.ok("删除成功");
    }
}




